package com.hrhx.tag;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.github.duhongming.highcharts.data.Data;
import com.github.duhongming.highcharts.json.GsonOption;
import com.github.duhongming.highcharts.series.Series;
import com.github.duhongming.highcharts.series.SeriesType;

public class HighChartsPie3DTag extends BodyTagSupport{
	private static final long serialVersionUID = 1L;
	private String id;
	private String title;
	private String subtitle;
	private Map<String,Double> seriesData;
	private String selectedData;
	private Boolean showDataLabels;
	@Override
	public int doStartTag() throws JspException {
		return BodyTag.EVAL_BODY_BUFFERED;
	}
	@SuppressWarnings("rawtypes")
	@Override
	public int doEndTag() throws JspException {
		//js开始位置
		String jsFuncStart="<script type=text/javascript>$(function () {$('#"+id+"').highcharts(";
		//js结束位置
		String jsFuncEnd=");});</script>";
		
		//BodyContent bc= this.getBodyContent();
		//String content =bc.getString();
		GsonOption option = new GsonOption();
		//设置标题、设置子标题
		option.title(title, subtitle);
		//设置3D状态
		option.chart().options3d().enabled(true).alpha(45).beta(0);
		option.chart().margin(new int[]{25,25,25,25})
					  .type(SeriesType.pie);
		//工具显示框
		option.tooltip().pointFormat("{series.name}: <b>{point.percentage:.2f}%</b>");
		//3D饼状图基本配置
		option.plotOptions().pie().allowPointSelect(true)
								  .cursor("pointer")
								  .depth(35)
								  .dataLabels().enabled(showDataLabels)
								               .format("<b>{point.name}</b>")//: {point.percentage:.2f}%
								               .distance(-25)
								               .style("fontWeight: 'bold',color: 'white',textShadow: '0px 1px 2px black'");
		//3D饼状图链接配置
		option.plotOptions().series().point().events().click("function () {window.open(this.options.url);}");
		//option.plotOptions().series().point().events().click("function () {location.href=this.options.url;}");
		if(seriesData!=null){
			Set<Entry<String, Double>>  set = seriesData.entrySet();
			Object[] obj= new Object[set.size()];
			int i=0;
			for (String key :seriesData.keySet()) {
				Object yValue=seriesData.get(key);
				Data data=new Data();
				//Jsoup解析a标签
				Document doc = Jsoup.parse(key);
				Elements a = doc.select("a[href]");
				if(a!=null&&!"".equalsIgnoreCase(a.text())){
					data.name(a.text());
				}else{
					data.name(key);
				}
				data.y(yValue).selected(false).sliced(false).url(a.attr("href"));
				
				if(selectedData!=null&&selectedData.equals(key)){
					data.selected(true);
					data.sliced(true);
				}
				obj[i]=data;
				i++;
			}
			Series series = new Series();
			series.name(subtitle).data(obj).type(SeriesType.pie);
			option.series(series);
		}
		
		try {
			this.pageContext.getOut().write(jsFuncStart+option.toString()+jsFuncEnd);
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return Tag.EVAL_PAGE;//继续处理页面
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getSubtitle() {
		return subtitle;
	}
	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}
	public Map<String, Double> getSeriesData() {
		return seriesData;
	}
	public void setSeriesData(Map<String, Double> seriesData) {
		this.seriesData = seriesData;
	}
	public String getSelectedData() {
		return selectedData;
	}
	public void setSelectedData(String selectedData) {
		this.selectedData = selectedData;
	}
	public Boolean getShowDataLabels() {
		return showDataLabels;
	}
	public void setShowDataLabels(Boolean showDataLabels) {
		this.showDataLabels = showDataLabels;
	}
	
}
